package lt.mii.optimum.components;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Group {
	private String $name;
	private Set<Student> $students;
	private int $level;



	public Group(String name) {
		$name = name.toUpperCase();
		$students = new HashSet<Student>();

		int x = Integer.parseInt($name.substring(0, 1));

		switch (x) {
		case 5:
			$level = 1;
			break;
		case 6:
			$level = 2;
			break;
		case 7:
			$level = 3;
			break;
		case 8:
			$level = 4;
			break;
		case 1:
			$level = 5;
			break;
		case 2:
			$level = 6;
			break;
		case 3:
			$level = 7;
			break;
		case 4:
			$level = 8;
			break;
		default:
			$level = -1;
		}
	}



	public Set<Student> exctractStudents(Lesson lesson) {
		Set<Student> ret = new HashSet<Student>();
		Iterator<Student> it = $students.iterator();
		while (it.hasNext()) {
			Student stud = it.next();
			if (stud.canStudy(lesson)) {
				ret.add(stud);
			} else {
				if (stud.needsToStudy(lesson.getSubject())) {
					ret.clear();
					break;
				}
			}
		}
		return ret;

	}



	public String getName() {
		return $name;
	}



	public void setName(String name) {
		this.$name = name;
	}



	public Set<Student> getStudents() {
		return $students;
	}



	public void addStudent(Student stud) {
		$students.add(stud);
	}



	public int getLevel() {
		return $level;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (($name == null) ? 0 : $name.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Group other = (Group) obj;
		if ($name == null) {
			if (other.$name != null)
				return false;
		} else if (!$name.equals(other.$name))
			return false;
		return true;
	}



	@Override
	public String toString() {
		return $name;
	}

}
